function OnLoad(self,mode)
	--print ("Loading GearBuddy frame " .. mode);
	self:RegisterEvent("PLAYER_EQUIPMENT_CHANGED");
--	self:RegisterEvent("PLAYER_TARGET_CHANGED");
--	if (IsAddOnLoaded("Blizzard_InspectUI")) then
--		SetUpInspectFrameOverlay();
--	else
--		self:RegisterEvent("ADDON_LOADED");
--	end
end

function SetUpInspectFrameOverlay()
		--print ("Creating Inspect Frame Overlay");
	        local GBInspectFrame = CreateFrame("Frame","iLvlInspectOverlay", InspectPaperDollItemsFrame);

                local InspectHeadString = GBInspectFrame:CreateFontString("InspectHeadSlotiLvlText","OVERLAY");
                InspectHeadString:SetPoint("TOPLEFT",InspectHeadSlot,"TOPLEFT",3,-3);
                InspectHeadString:SetFontObject("NumberFont_Outline_Large");

                local InspectNeckString = GBInspectFrame:CreateFontString("InspectNeckSlotiLvlText","OVERLAY");
                InspectNeckString:SetPoint("TOPLEFT",InspectNeckSlot,"TOPLEFT",3,-3);
                InspectNeckString:SetFontObject("NumberFont_Outline_Large");

                local InspectShoulderString = GBInspectFrame:CreateFontString("InspectShoulderSlotiLvlText","OVERLAY");
                InspectShoulderString:SetPoint("TOPLEFT",InspectShoulderSlot,"TOPLEFT",3,-3);
                InspectShoulderString:SetFontObject("NumberFont_Outline_Large");

                local InspectBackString = GBInspectFrame:CreateFontString("InspectBackSlotiLvlText","OVERLAY");
                InspectBackString:SetPoint("TOPLEFT",InspectBackSlot,"TOPLEFT",3,-3);
                InspectBackString:SetFontObject("NumberFont_Outline_Large");

                local InspectChestString = GBInspectFrame:CreateFontString("InspectChestSlotiLvlText","OVERLAY");
                InspectChestString:SetPoint("TOPLEFT",InspectChestSlot,"TOPLEFT",3,-3);
                InspectChestString:SetFontObject("NumberFont_Outline_Large");

                local InspectWristString = GBInspectFrame:CreateFontString("InspectWristSlotiLvlText","OVERLAY");
                InspectWristString:SetPoint("TOPLEFT",InspectWristSlot,"TOPLEFT",3,-3);
                InspectWristString:SetFontObject("NumberFont_Outline_Large");

                local InspectHandsString = GBInspectFrame:CreateFontString("InspectHandsSlotiLvlText","OVERLAY");
                InspectHandsString:SetPoint("TOPLEFT",InspectHandsSlot,"TOPLEFT",3,-3);
                InspectHandsString:SetFontObject("NumberFont_Outline_Large");

                local InspectWaistString = GBInspectFrame:CreateFontString("InspectWaistSlotiLvlText","OVERLAY");
                InspectWaistString:SetPoint("TOPLEFT",InspectWaistSlot,"TOPLEFT",3,-3);
                InspectWaistString:SetFontObject("NumberFont_Outline_Large");

                local InspectLegsString = GBInspectFrame:CreateFontString("InspectLegsSlotiLvlText","OVERLAY");
                InspectLegsString:SetPoint("TOPLEFT",InspectLegsSlot,"TOPLEFT",3,-3);
                InspectLegsString:SetFontObject("NumberFont_Outline_Large");

                local InspectFeetString = GBInspectFrame:CreateFontString("InspectFeetSlotiLvlText","OVERLAY");
                InspectFeetString:SetPoint("TOPLEFT",InspectFeetSlot,"TOPLEFT",3,-3);
                InspectFeetString:SetFontObject("NumberFont_Outline_Large");

                local InspectFinger0String = GBInspectFrame:CreateFontString("InspectFinger0SlotiLvlText","OVERLAY");
                InspectFinger0String:SetPoint("TOPLEFT",InspectFinger0Slot,"TOPLEFT",3,-3);
                InspectFinger0String:SetFontObject("NumberFont_Outline_Large");

                local InspectFinger1String = GBInspectFrame:CreateFontString("InspectFinger1SlotiLvlText","OVERLAY");
                InspectFinger1String:SetPoint("TOPLEFT",InspectFinger1Slot,"TOPLEFT",3,-3);
                InspectFinger1String:SetFontObject("NumberFont_Outline_Large");

                local InspectTrinket0String = GBInspectFrame:CreateFontString("InspectTrinket0SlotiLvlText","OVERLAY");
                InspectTrinket0String:SetPoint("TOPLEFT",InspectTrinket0Slot,"TOPLEFT",3,-3);
                InspectTrinket0String:SetFontObject("NumberFont_Outline_Large");

                local InspectTrinket1String = GBInspectFrame:CreateFontString("InspectTrinket1SlotiLvlText","OVERLAY");
                InspectTrinket1String:SetPoint("TOPLEFT",InspectTrinket1Slot,"TOPLEFT",3,-3);
                InspectTrinket1String:SetFontObject("NumberFont_Outline_Large");

                local InspectMainHandString = GBInspectFrame:CreateFontString("InspectMainHandSlotiLvlText","OVERLAY");
                InspectMainHandString:SetPoint("TOPLEFT",InspectMainHandSlot,"TOPLEFT",3,-3);
                InspectMainHandString:SetFontObject("NumberFont_Outline_Large");

                local InspectSecondaryHandString = GBInspectFrame:CreateFontString("InspectSecondaryHandSlotiLvlText","OVERLAY");
                InspectSecondaryHandString:SetPoint("TOPLEFT",InspectSecondaryHandSlot,"TOPLEFT",3,-3);
                InspectSecondaryHandString:SetFontObject("NumberFont_Outline_Large");

                local InspectRangedString = GBInspectFrame:CreateFontString("InspectRangedSlotiLvlText","OVERLAY");
                InspectRangedString:SetPoint("TOPLEFT",InspectRangedSlot,"TOPLEFT",3,-3);
                InspectRangedString:SetFontObject("NumberFont_Outline_Large");

                local InspectAverageString = GBInspectFrame:CreateFontString("InspectAverageiLvlText","OVERLAY");
                InspectAverageString:SetPoint("TOPLEFT",InspectRangedSlot,"TOPLEFT",-120,27);
                InspectAverageString:SetFontObject("NumberFont_Outline_Large");
                
--		GBInspectFrame:SetScript("OnShow",iLvl("target"));

end

function OnEvent(self, event, ...)
	if (event == "PLAYER_EQUIPMENT_CHANGED") then
		iLvl("player");
--	elseif (event == "PLAYER_TARGET_CHANGED") then
		--also check here that inspect frame is visible before enabling the running of the iLvl function for target
		--iLvl("target");
--	elseif (event == "ADDON_LOADED" and IsAddOnLoaded("Blizzard_InspectUI")) then
--		SetUpInspectFrameOverlay();
	end
end

function iLvl(mode)
	--Our frame is a child of the paperdoll frame and each slot now has an overlay text box to hold the 
	--ilvl of the item in its slot
	--print ("Running iLvl for " .. mode);
	local invSlot; -- 1 to 18
	local itemID;
	local itemSubtype;
	local totaliLvl = 0;
	local avgiLvl = 0;
	local numSlots = 17; -- for calculating the average
	local itemNamesBySlot = {};
	local itemLevelBySlot = {};
	for invSlot=1,18 do
		itemID = GetInventoryItemID(mode,invSlot);
		if itemID then
			local itemName,_,_,itemLevel,_,_,itemSubtype,_,_,_,_ = GetItemInfo(itemID);
			itemNamesBySlot[invSlot] = itemName;
			itemLevelBySlot[invSlot] = itemLevel;
			--print (mode .. " - Found " .. itemName .. " with ilvl " .. itemLevel .. " in slot " .. invSlot);
			if (invSlot == 16) then 
				--if Main Hand is two-hander, only divide by 16 (offhand will be zero - fury warriors taken care of below)
				if (string.match(itemSubtype,"Two") or string.match(itemSubtype,"Stave") or string.match(itemSubtype,"Polearm") or string.match(itemSubtype,"Fishing")) then
					--print ("Found two handed weapon");
					numSlots = 16;
				end
				totaliLvl = totaliLvl + itemLevel;
			elseif (invSlot == 17) then
				--if we have something in this slot then set numslots to 17
				--even if we had set it to 16 by finding a two-hander in the main-hand
				--takes care of warrriors dual-wielding two-handers
				numSlots = 17;
				totaliLvl = totaliLvl + itemLevel;
			elseif (invSlot ~= 4) then
				--Shirt is in slot 4 - skip it for iLvl calculations
				totaliLvl = totaliLvl + itemLevel;
			end
		else 
			itemNamesBySlot[invSlot] = "Missing";
                        itemLevelBySlot[invSlot] = 0;
			--print (mode .. " - Skipping slot " .. invSlot .. " (Nothing there)");
		end
	end
	avgiLvl = math.floor(totaliLvl/numSlots);
	if (mode == "target") then
		InspectHeadSlotiLvlText:SetFormattedText(itemLevelBySlot[1]);
		InspectNeckSlotiLvlText:SetFormattedText(itemLevelBySlot[2]);
                InspectShoulderSlotiLvlText:SetFormattedText(itemLevelBySlot[3]);
                InspectBackSlotiLvlText:SetFormattedText(itemLevelBySlot[15]);
                InspectChestSlotiLvlText:SetFormattedText(itemLevelBySlot[5]);
                InspectWristSlotiLvlText:SetFormattedText(itemLevelBySlot[9]);
                InspectHandsSlotiLvlText:SetFormattedText(itemLevelBySlot[10]);
                InspectWaistSlotiLvlText:SetFormattedText(itemLevelBySlot[6]);
                InspectLegsSlotiLvlText:SetFormattedText(itemLevelBySlot[7]);
                InspectFeetSlotiLvlText:SetFormattedText(itemLevelBySlot[8]);
                InspectFinger0SlotiLvlText:SetFormattedText(itemLevelBySlot[11]);
                InspectFinger1SlotiLvlText:SetFormattedText(itemLevelBySlot[12]);
                InspectTrinket0SlotiLvlText:SetFormattedText(itemLevelBySlot[13]);
                InspectTrinket1SlotiLvlText:SetFormattedText(itemLevelBySlot[14]);
                InspectMainHandSlotiLvlText:SetFormattedText(itemLevelBySlot[16]);
                InspectSecondaryHandSlotiLvlText:SetFormattedText(itemLevelBySlot[17]);
                InspectRangedSlotiLvlText:SetFormattedText(itemLevelBySlot[18]);
		InspectAverageiLvlText:SetFormattedText("Average iLvl (equipped): " .. avgiLvl);
	else
		HeadSlotiLvlText:SetFormattedText(itemLevelBySlot[1]);
		NeckSlotiLvlText:SetFormattedText(itemLevelBySlot[2]);
		ShoulderSlotiLvlText:SetFormattedText(itemLevelBySlot[3]);
		BackSlotiLvlText:SetFormattedText(itemLevelBySlot[15]);
		ChestSlotiLvlText:SetFormattedText(itemLevelBySlot[5]);
		WristSlotiLvlText:SetFormattedText(itemLevelBySlot[9]);
		HandsSlotiLvlText:SetFormattedText(itemLevelBySlot[10]);
		WaistSlotiLvlText:SetFormattedText(itemLevelBySlot[6]);
		LegsSlotiLvlText:SetFormattedText(itemLevelBySlot[7]);
		FeetSlotiLvlText:SetFormattedText(itemLevelBySlot[8]);
		Finger0SlotiLvlText:SetFormattedText(itemLevelBySlot[11]);
		Finger1SlotiLvlText:SetFormattedText(itemLevelBySlot[12]);
		Trinket0SlotiLvlText:SetFormattedText(itemLevelBySlot[13]);
		Trinket1SlotiLvlText:SetFormattedText(itemLevelBySlot[14]);
		MainHandSlotiLvlText:SetFormattedText(itemLevelBySlot[16]);
		SecondaryHandSlotiLvlText:SetFormattedText(itemLevelBySlot[17]);
		RangedSlotiLvlText:SetFormattedText(itemLevelBySlot[18]);
		averageiLvlText:SetFormattedText("Average iLvl (equipped): " .. avgiLvl);
	end
end
